package com.wj.controller;

import com.wj.pojo.Admin;
import com.wj.pojo.RespBean;
import com.wj.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author wj
 * @Date 2022/7/30 16:36
 * @Description: TODO
 * @Version 1.0
 */

@Api(tags = "操作员信息修改")
@RestController
@RequestMapping("/admin")
public class AdminInfoController {

    @Resource
    private IAdminService adminService;

    @ApiOperation(value = "更新用户头像")
    @PostMapping("/userface")
    public RespBean updateUserFace(MultipartFile file, Integer id) {
        return adminService.updateUserFace(file, id);
    }

    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/info")
    public RespBean updateAdminInfo(@RequestBody Admin admin, Authentication authentication) {
        if (adminService.updateById(admin)) {
            UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken
                    (admin, authentication.getCredentials(), admin.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(token);
            return RespBean.success("修改成功");
        }
        return RespBean.success("修改失败");
    }

    @ApiOperation(value = "更新密码")
    @PutMapping("/pass")
    public RespBean updatePassword(@RequestBody Map<String, Object> map) {
        return adminService.updatePassword((Integer) map.get("adminId"),
                (String) map.get("oldPass"), (String) map.get("pass"));
    }

}
